//
//  RTLeftMenuViewController.m
//  WeiLiToy_Example
//
//  Created by baxiang on 2018/1/18.
//  Copyright © 2018年 baxiang. All rights reserved.
//

#import "RTLeftMenuViewController.h"
#import "RTAppDelegate.h"
#import "RBSideMenuController.h"
#define RTSideMenuWidth [UIScreen mainScreen].bounds.size.width-50

@interface RTLeftMenuViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UIView *headBackview;
@property (nonatomic, strong) UITableView *mainTableView;
@property (nonatomic, strong) UIImageView *headImageView;
@property (nonatomic,strong) NSMutableArray *deviceArray;
@end

@implementation RTLeftMenuViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupHeadUserView];
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero ];
    tableView.showsVerticalScrollIndicator = NO;
    tableView.delegate = self;
    tableView.dataSource = self;
    [self.view addSubview:tableView];
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.headBackview.mas_bottom);
        make.left.mas_equalTo(0);
        make.width.mas_equalTo([UIScreen mainScreen].bounds.size.width-50);
        make.bottom.mas_equalTo(0);
    }];
    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:NSStringFromClass([UITableViewCell class])];
    _mainTableView = tableView;
    tableView.tableFooterView = [UIView new];
    [self fetchDeviceList];
}

- (void )setupHeadUserView {
    UIButton *headBackview = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, RTSideMenuWidth, 155)];
    [self.view addSubview:headBackview];
    [headBackview addTarget:self action:@selector(userInfoHandle) forControlEvents:UIControlEventTouchUpInside];
    self.headBackview = headBackview;
    
    UIImageView *headImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
    headImageView.layer.cornerRadius = 35;
    headImageView.layer.masksToBounds = YES;
    headImageView.image = [UIImage imageNamed:@"icon_head_default"];
    [headBackview addSubview:headImageView];
    [headImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(53);
        make.left.mas_equalTo(25);
        make.width.height.mas_equalTo(70);
    }];
    _headImageView = headImageView;
    UILabel *nameLabel = [[UILabel alloc] init];
    nameLabel.font = [UIFont systemFontOfSize:15];
    [headBackview addSubview:nameLabel];
    nameLabel.text = @"退出登录";
    UIImageView *arrow = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"button_next"]];
    [headBackview addSubview:arrow];
    [arrow mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-23);
        make.centerY.mas_equalTo(headImageView.mas_centerY);
        make.width.mas_equalTo(arrow.image.size.width);
        make.height.mas_equalTo(arrow.image.size.height);
        
    }];
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(headImageView.mas_right).offset(20);
        make.centerY.mas_equalTo(headImageView.mas_centerY);
        make.right.mas_equalTo(arrow.mas_left).offset(-5);
    }];
}

-(void)userInfoHandle{
    
    [RBAccessConfig clearLoginUserData];
     [(RTAppDelegate*)[UIApplication sharedApplication].delegate switchRootViewController];
}

-(NSMutableArray *)deviceArray
{
    if(!_deviceArray){
        _deviceArray = [NSMutableArray new];
    }
    return _deviceArray;
}

-(void)fetchDeviceList
{
    [RBDeviceApi getDeviceList:NO block:^(NSArray<RBDeviceModel*> *devices, NSError *error){
        if (devices&&!error) {
             [self.deviceArray addObjectsFromArray:devices];
             [self.mainTableView reloadData];
        }
    }];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _deviceArray.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 5;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
    
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
   UITableViewCell *cell= [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class])];
   cell.backgroundColor = [UIColor clearColor];
    RBDeviceModel *deviceModel= [_deviceArray objectAtIndex:indexPath.section];
    if(indexPath.row==0){
         cell.textLabel.text = [NSString stringWithFormat:@"id:%@",deviceModel.deviceID];
    }
    else if(indexPath.row==1){
        cell.textLabel.text = [NSString stringWithFormat:@"name:%@",deviceModel.name];
    }else if (indexPath.row ==2){
        cell.textLabel.text = [NSString stringWithFormat:@"battery:%ld",deviceModel.battery];
    }else if (indexPath.row ==3){
         cell.textLabel.text = [NSString stringWithFormat:@"online:%d",deviceModel.online];
    }else if (indexPath.row ==4){
        cell.textLabel.text = [NSString stringWithFormat:@"manager:%d",deviceModel.manager];
    }
     return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.row==0){
        RBDeviceModel *deviceModel= [_deviceArray objectAtIndex:indexPath.section];
        [RBAccessConfig saveDeviceID:deviceModel.deviceID];
        [self.sideMenuViewController hideMenuViewController];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
